<template>
  <div class="categories">
    <div class="categories_const">
      <el-button type="primary" @click="addCategories">添加商品</el-button>
      <zk-table show-index index-text="#" :data="tabledata" :columns="columns" :expand-type="false"
        :selection-type="false" border max-height="500px">
        <template slot="status" scope="scope">
          <span :class="
            scope.row.cat_deleted == false
              ? 'el-icon-success'
              : 'el-icon-error'
          "></span>
        </template>
        <template slot="sort" scope="scope">
          <el-tag :type="
            scope.row.cat_level == 0
              ? ''
              : scope.row.cat_level == 1
                ? 'success'
                : 'danger'
          ">
            {{
                scope.row.cat_level == 0
                  ? "一级"
                  : scope.row.cat_level == 1
                    ? "二级"
                    : "三级"
            }}
          </el-tag>
        </template>
        <template slot="caot" scope="scope">
          <el-button type="text" size="small" @click="updateCate(scope.row)">编辑</el-button>
          <el-button type="text" size="small" @click="del(scope.row)">删除</el-button>
        </template>
      </zk-table>
      <!-- 点击添加模态框 -->

      <el-dialog title="添加分类" :visible.sync="addCategoriesStatus" width="30%" :before-close="handleClose">
        <span>
          <el-form :model="form" :rules="rules" label-width="100px" class="demo-ruleForm">
            <el-form-item label="活动名称" prop="cateName">
              <el-input v-model="form.cateName"></el-input>
            </el-form-item>
            <el-form-item label="父级分类">
              <el-cascader v-model="opCateName" :options="options" :props="props_a" clearable ref="getDemcate"
                @change="handleChange"></el-cascader>
            </el-form-item>
          </el-form>
        </span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addCategoriesStatus = false">取 消</el-button>
          <el-button type="primary" @click="sureadd">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 点击编辑 -->
      <el-dialog title="编辑分类" :visible.sync="addCategoriesStatus_b" width="30%" :before-close="handleClose">
        <span>
          <el-form :model="forms" label-width="100px" class="demo-ruleForm">
            <el-form-item label="活动名称" prop="cateName">
              <el-input v-model="forms.cateName_b"></el-input>
            </el-form-item>
          </el-form>
        </span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="updateSure">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 分页操作 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[5, 10, 15, 20]"
        :page-size="info.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="tabledata.length">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import {
  getcategories,
  addCatecategories,
  updatecategories,
  Delcategories,
} from "@/utils/http";
export default {
  data() {
    return {
      tabledata: [],
      // 点击添加参数状态
      addCategoriesStatus: false,
      addCategoriesStatus_b: false,
      // 选择器
      options: [],
      opCateName: "",
      cat_level: 0,
      myCat_id: 0,
      // 编辑id
      myupID: 0,
      props_a: {
        value: "cat_id",
        label: "cat_name",
        expandTrigger: "hover",
        checkStrictly: true,
      },
      info: {
        pagenum: 1,
        pagesize: 10,
      },
      columns: [
        {
          label: "分类名称",
          prop: "cat_name",
        },
        {
          label: "是否有效",
          type: "template",
          template: "status",
        },
        {
          label: "排序",
          type: "template",
          template: "sort",
        },
        {
          label: "操作",
          type: "template",
          template: "caot",
        },
      ],
      form: {
        cateName: "",
      },
      forms: {
        cateName_b: "",
      },
      rules: {
        cateName: [
          // { required: true, message: "请输入活动名称", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.addcategetcategories();
  },
  methods: {
    // 点击删除
    del(val) {
      console.log(val.cat_id);
      Delcategories(val.cat_id).then((res) => {
        console.log(res);
        this.$message({
          message: "恭喜你，删除成功",
          type: "success",
        });
        this.addcategetcategories();
      });
    },
    // 编辑确定
    updateSure() {
      this.addCategoriesStatus_b = false;
      let obj = {
        id: this.myupID,
        cat_name: this.forms.cateName_b,
      };
      updatecategories(obj).then((res) => {
        console.log(res);
        this.addcategetcategories();
      });
    },
    // 点击编辑
    updateCate(row) {
      this.myupID = row.cat_id;
      console.log(row);
      this.addCategoriesStatus_b = true;
      this.forms.cateName_b = row.cat_name;
    },
    // 封装调用接口
    addcategetcategories() {
      getcategories().then((res) => {
        console.log(res);
        this.tabledata = res.data;
      });
    },
    // 点击确定
    sureadd() {
      this.addCategoriesStatus = false;
      let obj = {
        cat_name: this.form.cateName,
        cat_level: this.cat_level,
        cat_pid: this.myCat_id,
      };
      console.log(obj);
      // 点击添加
      addCatecategories(obj).then((res) => {
        console.log(res);
        this.addcategetcategories();
      });
      this.form.cateName = "";
    },
    // 选择器事件
    handleChange(val) {
      console.log(val);
      if (val.length != 0) {
        this.myCat_id = val.at(-1);
        this.cat_level = val.length;
      } else if (val.length == 0) {
        this.myCat_id = 0;
        this.cat_level = 0;
      }
      this.$refs.getDemcate.dropDownVisible = false;
    },
    // 当前页码
    handleCurrentChange(val) {
      this.info.pagenum = val;
      // this.addcategetcategories();
    },
    // 当前每页数量
    handleSizeChange(val) {
      this.info.pagesize = val;
      // this.addcategetcategories();
    },
    // 点击添加参数
    addCategories() {
      this.addCategoriesStatus = true;
      getcategories().then((res) => {
        console.log(res);
        this.options = res.data;
      });
    },
    // 关闭模态框事件
    handleClose() {
      this.addCategoriesStatus_b = false;
      this.options = [];
    },
  },
};
</script>
<style>
.el-radio {
  position: absolute;
  width: 100%;
  height: 30px;
}

.el-radio__input {
  display: none;
}
</style>
<style lang="scss" scoped>
.el-pagination {
  margin-top: 10px;
}

.zk-table {
  height: 509px;
  margin-top: 10px;
  max-height: 540px;
}

.categories_const {
  padding: 15px;
  background: white;
  border-radius: 8px;
}

.el-icon-success {
  color: #90ee90;
}

.el-icon-error {
  color: red;
}
</style>